package com.fanxuankai.boot.monitor;

import com.dingtalk.api.request.OapiRobotSendRequest;
import com.fanxuankai.boot.monitor.autoconfigure.MonitorProperties;
import com.fanxuankai.commons.util.DingTalkClientHelper;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;

/**
 * 钉钉
 *
 * @author fanxuankai
 */
public class DingTalkNotifier implements Notifier<Message> {
    private final DingTalkClientHelper helper;
    private final MonitorProperties.DingTalk dingTalk;

    public DingTalkNotifier(MonitorProperties.DingTalk dingTalk) {
        this.dingTalk = dingTalk;
        this.helper = DingTalkClientHelper.newInstance(dingTalk.getUrl(), dingTalk.getAccessToken(), dingTalk.getSecret());
    }

    @Override
    public void notice(Message message) {
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        if (!CollectionUtils.isEmpty(dingTalk.getAtMobiles())) {
            at.setAtMobiles(new ArrayList<>(dingTalk.getAtMobiles()));
        }
        at.setIsAtAll(dingTalk.isAtAll());
        helper.sendRobotMarkdown(message.getTitle(), markdown(message), at);
    }
}
